package com.weigongbao.weigongbao;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CallBack<Details> {

    private String URL = "http://weigongbao.boguyuan.com/weigongbao/api/storeProduct_view?pid=212";
    private RecyclerView mRecy;
    private TextView mName;
    private LinearLayout mLinear;
    private TextView num;
    private TextView mPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        //请求网络
        requestNet();
    }

    private void init() {
        mName = (TextView) findViewById(R.id.details_name);
        mLinear = (LinearLayout) findViewById(R.id.details_linear);
        num = (TextView) findViewById(R.id.details_num);
        mPrice = (TextView) findViewById(R.id.details_price);

        mRecy = (RecyclerView) findViewById(R.id.details_recy);
        mRecy.setLayoutManager(new GridLayoutManager(this, 3));
        mRecy.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(10, 5,0, 5);
            }
        });
    }

    private void requestNet() {
        VolleyUtils volley = VolleyUtils.initVolley();
        volley.get(URL, Details.class, this);
    }

    @Override
    public void succeed(Details details) {
        setAdapter(details.getData().getImgList());//图片展示
        addNorms(details.getData().getSpecitems());//规格
    }

    //库存
    public void setNum(int nums) {
        num.setText("库存  " + nums);
    }

    public void setPrice(String price) {
        mPrice.setText("微商代理价  " + price);
    }

    //添加规格属性
    public void addNorms(List<Details.DataBean.SpecitemsBean> list) {
        for (int i = 0; i < list.size(); i++) {
            TextView view = new TextView(this);
            if (i == 0) {
                setBackGround(view, R.drawable.button_backgrout);
                setColor(view, R.color.colorGreen);
                //设置库存 默认为第一条
                setNum(list.get(i).getStock());
                //价格
                setPrice(list.get(i).getSpecPrice());

            } else {
                setBackGround(view, R.drawable.textview_backgrout);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(20, 0, 0, 0);
            view.setPadding(20, 10, 20, 10);
            view.setText(list.get(i).getSpecTitle());
            view.setTextSize(18);
            mLinear.addView(view, params);
        }
    }

    //设置适配器
    public void setAdapter(List<String> list) {
        mRecy.setAdapter(new MyAdapter(this, (ArrayList<String>) list));
    }

    private void setBackGround(View view, int res) {
        view.setBackground(getResources().getDrawable(res));
    }

    private void setColor(TextView view, int color) {
        view.setTextColor(getResources().getColor(color));
    }

    @Override
    public void fail(String e) {

    }
}
